package com.lktx.bbfactory.common;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.lktx.bbfactory.common.exception.BizAsserts;
import lombok.Data;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分页数据封装类
 */
@Data
public class CommonPage<T> {
    private Integer pageNum = 1;
    private Integer pageSize = 20;
    private Integer totalPage = 0;
    private Long total = 0L;
    private List<T> list;


    /**
     * MybatisPlus分页信息转换为通用分页
     *
     * @param pageInfo
     * @param <T>
     * @return
     */
    public static <T> CommonPage<T> restPage(IPage<T> pageInfo) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage((int) pageInfo.getPages());
        result.setPageNum((int) pageInfo.getCurrent());
        result.setPageSize((int) pageInfo.getSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getRecords());
        return result;
    }

    /**
     * 组装返回通用分页信息
     *
     * @param list
     * @param total
     * @param current
     * @param pageSize
     * @param <T>
     * @return
     */
    public static <T> CommonPage<T> restPage(List<T> list, long total, long current, long pageSize) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage((int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1));
        result.setPageNum((int) current);
        result.setPageSize((int) pageSize);
        result.setTotal(total);
        result.setList(list);
        return result;
    }


    /**
     * 返回空分页
     *
     * @param <T>
     * @return
     */
    public static <T> CommonPage<T> empty() {
        CommonPage<T> result = new CommonPage<T>();
        result.setList(new LinkedList<>());
        return result;
    }


    /**
     * 转换mybatis-plus分页结果到指定类型的CommonPage
     *
     * @param iPage
     * @param recordConvertor
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T, R> CommonPage<R> convert(IPage<T> iPage, Function<T, R> recordConvertor) {
        BizAsserts.notNull(iPage, "iPage不能为空");
        List<R> convertedRecordList = iPage.getRecords().stream()
                .map(recordConvertor)
                .collect(Collectors.toList());
        CommonPage<R> commonPage = new CommonPage<>();
        commonPage.setPageNum((int) iPage.getCurrent());
        commonPage.setPageSize((int) iPage.getSize());
        commonPage.setTotal(iPage.getTotal());
        commonPage.setTotalPage((int) iPage.getPages());
        commonPage.setList(convertedRecordList);
        return commonPage;
    }

    /**
     * 转换CommonPage中的数据类型
     *
     * @param page
     * @param recordConvertor
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T, R> CommonPage<R> convert(CommonPage<T> page, Function<T, R> recordConvertor) {
        BizAsserts.notNull(page, "page不能为空");
        List<R> convertedRecordList = page.getList().stream()
                .map(recordConvertor)
                .collect(Collectors.toList());
        CommonPage<R> commonPage = new CommonPage<>();
        commonPage.setPageNum(page.getPageNum());
        commonPage.setPageSize(page.getPageSize());
        commonPage.setTotal(page.getTotal());
        commonPage.setTotalPage(page.getTotalPage());
        commonPage.setList(convertedRecordList);
        return commonPage;
    }


}
